<template>
    <div class="search-list">
        <Header>
            <template v-slot:icon-l><i class="iconfont icon-fanhui" @click="goBack"></i></template>
            <div class="search-box">
                <i class="iconfont icon-search"></i>
                <form action="" onsubmit="return false" @keyup.enter="getData">
                    <input type="search" placeholder="搜您喜欢的产品..." class="search-txt" v-model="searchValue">
                </form>
            </div>
            <template v-slot:icon-r><span @click="getData">搜索</span></template>
        </Header>
        <ul class="navbar">
            <li v-for="(item,index) of searchList.data" :key="index" @click="changeTab(index)">
                <div :class="{active:index == searchList.currentIndex}">{{item.name}}</div>
                <span class="icon" v-if="index">
                    <i class="iconfont icon-shangjiantou1" :class="{active:item.status == 1}"></i>
                    <i class="iconfont icon-xiajiantou" :class="{active:item.status == 2}"></i>
                </span>
            </li>
        </ul>
        <section>
            <ul v-if="goodsList.length">
                <li v-for="(item,index) of goodsList" :key="index">
                    <img v-lazy="item.imgUrl" alt="">
                    <h3>{{item.name}}</h3>
                    <div class="price">
                        <div class="price-num">
                            <span>¥</span>
                            <b>{{item.price}}</b>
                        </div>
                        <div class="purchase">立即购买</div>
                    </div>
                </li>
            </ul>
            <h2 v-else>暂无数据...</h2>
        </section>
    </div>
</template>

<script>
import Header from '@/components/common/Header.vue'
export default {
    components: {
        Header
    },
    data () {
        return {
            searchValue:this.$route.query.key,
            goodsList:[],
            searchList:{
                currentIndex:0,
                data:[
                {name:'综合'},
                {name:'价格',status:0,key:'price'},
                {name:'销量',status:0,key:'num'}
                ]
            }
        }
    },
    computed: {
        orderBy(){
            if(!this.searchList.currentIndex)return
            //获取当前对象
            let obj = this.searchList.data[this.searchList.currentIndex]
            //判断升序降序
            let val = obj.status == 1? 'asc': 'desc'
            return {
                key:obj.key,
                rule:val
            }
        }   
    },
    watch: {},
    methods: {
        //返回箭头
        goBack(){
            this.$router.back()
        },
        //请求数据
        getData(){
            this.$axios({
                url:'/api/goods/shopList',
                params:{
                    searchValue:this.searchValue,
                    orderBy:this.orderBy
                }
            }).then(res=>{
                this.goodsList = res.data.data
            })
            //避免重复搜索
            if(this.$route.query.key == this.searchValue) return
            this.$router.push({name:'search-list',query:{key:this.searchValue}})
        },
        //切换标题
        changeTab(index){
            this.searchList.currentIndex = index
            //箭头高亮
            //除了当前点击这个其他都为0
            this.searchList.data.forEach((v,i)=>{
                if(i!==index){
                    v.status = 0
                }
            })
            //切换箭头
            let item = this.searchList.data[index].status
            this.searchList.data[index].status = item == 1? 2 : 1 
            //发送请求
            this.getData()
        }
    },
    created () {
        this.getData()
    },
    mounted () {},
    beforeCreate () {},
    beforeMount () {},
    beforeUpdate () {},
    updated () {},
    beforeDestroy () {},
    destroyed () {},
    activated () {}
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.search-list{
    display: flex;
    flex-direction: column;
    overflow: hidden;
    width: 100vw;
    height: calc(100vh - 1.6rem);
    .header{
        ::v-deep .icon-l{
            width: auto;
        }        
        form{
            flex:1;
            padding-right: .5333rem;
            .search-txt{
                width: 100%;
                color: #000;
            }
        }
    }
    .navbar{
        display: flex;
        align-items: center;
        justify-content: space-around;
        padding: .4rem 0;
        li{
            display: flex;
            align-items: center;
            .icon{
                display: flex;
                flex-direction: column;
                .iconfont{
                    font-size: .32rem;
                    padding-left: .1333rem;
                }
            }
            .active{
                color: red;
            }
        }
    }
    section{
        flex: 1;
        overflow: hidden;
        ul{
            display: flex;
            flex-wrap: wrap;
            li{
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                width: 50%;
                img{
                    width: 4.5333rem;
                    height: 4.5333rem;
                }
                h3{
                    box-sizing: border-box;
                    padding: 0 .5333rem;
                    margin-bottom: .16rem;
                    width: 100%;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    color: #222;
                    font-weight: 400;
                }
                .price{
                    display: flex;
                    justify-content: space-between;
                    align-items: baseline;
                    width: 100%;
                    box-sizing: border-box;
                    padding: 0 .5333rem;
                    font-size: .3733rem;
                    .purchase{
                        background-color: #b0352f;
                        color: #fff;
                        padding: .08rem .16rem;
                        border-radius: .16rem;
                    }
                    .price-num{
                        color: #b0352f;
                        span{
                            font-size: .32rem;
                        }
                        b{
                            font-size: .4267rem;
                        }
                    }
                }
            }
        }
    }
}
</style>
